//
//  wupinTopView.m
//  商城
//
//  Created by runone02 on 2018/8/27.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "wupinTopView.h"

@interface wupinTopView ()

/* 左边Item */
@property (strong , nonatomic)UIButton *wupinleftItemButton;

/* 右边第二个Item */
@property (strong , nonatomic)UIButton *wupinrightRItemButton;
/* 搜索 */
@property (strong , nonatomic)UIView *wupintopSearchView;
/* 搜索按钮 */
@property (strong , nonatomic)UIButton *wupinsearchButton;
/* 语音按钮 */
@property (strong , nonatomic)UIButton *wupinvoiceButton;
/* 通知 */
@property (weak ,nonatomic) id wupindcObserve;
@end

@implementation wupinTopView

#pragma mark - Intial
- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpUI];
        
        [self acceptanceNote];
    }
    return self;
}

- (void)setUpUI {
    self.backgroundColor = [UIColor clearColor];
    
    _wupinleftItemButton = ({
        UIButton * button = [UIButton new];
        [button setImage:IMAGE(@"navigation_back_normal") forState:UIControlStateNormal];
        [button addTarget:self action:@selector(leftButtonItemClick) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    

    
    _wupinrightRItemButton = ({
        UIButton * button = [UIButton new];
        [button setImage:IMAGE(@"tabbar_icon_message") forState:UIControlStateNormal];
        [button addTarget:self action:@selector(rightRButtonItemClick) forControlEvents:UIControlEventTouchUpInside];
        button;
    });

    [self addSubview:_wupinrightRItemButton];
    [self addSubview:_wupinleftItemButton];
    
    CAGradientLayer * layer = [[CAGradientLayer alloc] init];
    layer.frame = self.bounds;
    layer.colors = @[(id)[UIColor colorWithWhite:0 alpha:0.2].CGColor,(id)[UIColor colorWithWhite:0 alpha:0.15].CGColor,(id)[UIColor colorWithWhite:0 alpha:0.1].CGColor,(id)[UIColor colorWithWhite:0 alpha:0.05].CGColor,(id)[UIColor colorWithWhite:0 alpha:0.03].CGColor,(id)[UIColor colorWithWhite:0 alpha:0.01].CGColor,(id)[UIColor colorWithWhite:0 alpha:0.0].CGColor];
    [self.layer addSublayer:layer];
    
    _wupintopSearchView = [[UIView alloc] init];
    _wupintopSearchView.backgroundColor = [UIColor whiteColor];
    _wupintopSearchView.layer.cornerRadius = 16;
    [_wupintopSearchView.layer masksToBounds];
    [self addSubview:_wupintopSearchView];
    
    _wupinsearchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_wupinsearchButton setTitle:@"搜索商品" forState:0];
    [_wupinsearchButton setTitleColor:[UIColor lightGrayColor] forState:0];
    _wupinsearchButton.titleLabel.font = TITLE_FONT(25);
    [_wupinsearchButton setImage:IMAGE(@"group_home_search_gray") forState:0];
    [_wupinsearchButton adjustsImageWhenHighlighted];
    _wupinsearchButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _wupinsearchButton.titleEdgeInsets = UIEdgeInsetsMake(0, 2 * DCMargin, 0, 0);
    _wupinsearchButton.imageEdgeInsets = UIEdgeInsetsMake(0, DCMargin, 0, 0);
    [_wupinsearchButton addTarget:self action:@selector(searchButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_wupintopSearchView addSubview:_wupinsearchButton];
    
    _wupinvoiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _wupinvoiceButton.adjustsImageWhenHighlighted = NO;
    [_wupinvoiceButton addTarget:self action:@selector(voiceButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_wupinvoiceButton setImage:IMAGE(@"icon_voice_search") forState:0];
    [_wupintopSearchView addSubview:_wupinvoiceButton];
}

#pragma mark - 接受通知
- (void)acceptanceNote{
    //滚动到详情
    WEAKSELF
    _wupindcObserve = [[NSNotificationCenter defaultCenter]addObserverForName:SHOWTOPTOOLVIEW object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        weakSelf.backgroundColor = [UIColor whiteColor];
        weakSelf.wupintopSearchView.backgroundColor = RGB(240, 240, 240);
        [weakSelf.wupinleftItemButton setImage:IMAGE(@"shouye_icon_scan_gray") forState:0];

        [weakSelf.wupinrightRItemButton setImage:IMAGE(@"icon_gouwuche_title_dark") forState:0];//
    }];
    
    _wupindcObserve = [[NSNotificationCenter defaultCenter]addObserverForName:HIDETOPTOOLVIEW object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        weakSelf.backgroundColor = [UIColor clearColor];
        weakSelf.wupintopSearchView.backgroundColor = [UIColor whiteColor];
        [weakSelf.wupinleftItemButton setImage:IMAGE(@"shouye_icon_scan_white") forState:0];
        [weakSelf.wupinrightRItemButton setImage:IMAGE(@"icon_gouwuche_title_white") forState:0];
    }];
}

#pragma mark - 布局
- (void)layoutSubviews{
    [super layoutSubviews];
    WEAKSELF
    [_wupinleftItemButton mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.equalTo(self.mas_top).offset(20);
        make.left.equalTo(self.mas_left).offset(0);
        make.height.equalTo(@44);
        make.width.equalTo(@44);
    }];

    
    [_wupinrightRItemButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.wupinleftItemButton.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-0);
        make.height.equalTo(@44);
        make.width.equalTo(@44);
    }];
    
    [_wupintopSearchView mas_makeConstraints:^(MASConstraintMaker *make) {
        [make.left.mas_equalTo(weakSelf.wupinleftItemButton.mas_right)setOffset:5];
        [make.right.mas_equalTo(weakSelf.wupinrightRItemButton.mas_left)setOffset:5];
        make.height.mas_equalTo(@(32));
        make.centerY.mas_equalTo(weakSelf.wupinrightRItemButton);
        
    }];
    
    [_wupinsearchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.wupintopSearchView);
        make.top.mas_equalTo(weakSelf.wupintopSearchView);
        make.height.mas_equalTo(weakSelf.wupintopSearchView);
        [make.right.mas_equalTo(weakSelf.wupintopSearchView)setOffset:-2*DCMargin];
    }];
    
    [_wupinvoiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakSelf.wupintopSearchView);
        make.centerY.mas_equalTo(weakSelf.wupintopSearchView);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
}

#pragma mark - 消失
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:_wupindcObserve];
}


#pragma 自定义左边导航Item点击
- (void)leftButtonItemClick {
    !_wupinleftItemClickBlock ? : _wupinleftItemClickBlock();
}

#pragma mark - 自定义右边第二个导航Item点击
- (void)rightRButtonItemClick{
    !_wupinrightRItemClickBlock ? : _wupinrightRItemClickBlock();
}

#pragma mark - 搜索按钮点击
- (void)searchButtonClick{
    !_wupinsearchButtonClickBlock ? : _wupinsearchButtonClickBlock();
}


#pragma mark - 语音按钮点击
- (void)voiceButtonClick{
    !_wupinvoiceButtonClickBlock ? : _wupinvoiceButtonClickBlock();
}


@end
